package com.smasher.widget.selection;

import android.content.Context;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.smasher.widget.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeSelection {

    private boolean showYear = true;
    private boolean showMonth = true;
    private boolean showDay = true;
    private boolean showHour;
    private boolean showMinute;
    private boolean showSecond;

    private int startYear;
    private int startMonth;
    private int startDay;

    private int endYear;
    private int endMonth;
    private int endDay;

    private OnTimeListener onTimeListener;

    private int index;


    public TimeSelection(TimeSelectBuilder builder) {
        this.startYear = builder.getStartYear();
        this.startMonth = builder.getStartMonth();
        this.startDay = builder.getStartDay();
        this.endYear = builder.getEndYear();
        this.endMonth = builder.getEndMonth();
        this.endDay = builder.getEndDay();

        this.showYear = builder.isShowYear();
        this.showMonth = builder.isShowMonth();
        this.showDay = builder.isShowDay();
        this.showHour = builder.isShowHour();
        this.showMinute = builder.isShowMinute();
        this.showSecond = builder.isShowSecond();
        this.onTimeListener = builder.getOnTimeListener();

        if (startYear == 0) {
            startYear = 1960;
        }
        if (startMonth == 0) {
            startMonth = 0;
        }
        if (startDay == 0) {
            startDay = 1;
        }

        if (endYear == 0) {
            endYear = 2050;
        }
        if (endMonth == 0) {
            endMonth = 0;
        }
        if (endDay == 0) {
            endDay = 1;
        }
    }


    private final OnTimeSelectListener onTimeSelectListener = (date, v) -> {
        String dateFormat = getTime(date);
        onTimeListener.onTimeSelected(index, dateFormat);
        onTimeListener.onDateSelected(index, date);
    };

    /**
     * 选择日期
     */
    public void showView(Context context, final int index) {
        this.index = index;

        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        //正确设置方式 原因：注意事项有说明
        startDate.set(startYear, startMonth, startDay);
        endDate.set(endYear, endMonth, endDay);

        TimePickerBuilder builder = new TimePickerBuilder(context, onTimeSelectListener);
        builder.setSubmitText(context.getString(R.string.app_ok));
        builder.setCancelText(context.getString(R.string.app_cancel));
        builder.setDate(selectedDate);
        builder.setRangDate(startDate, endDate);
        builder.setType(new boolean[]{showYear, showMonth, showDay, showHour, showMinute, showSecond});
        TimePickerView timePickerView = builder.build();
        timePickerView.show();
    }

    private String getTime(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return dateFormat.format(date);
    }

}
